﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using NativeExcel;
using System.Reflection;
using System.ComponentModel;
using Sinacor.Infra.UI.Common;
using System.Windows;
using PSE.Framework.UI.Resources;
using System.Collections;
using Sinacor.Infra.Common;

namespace Sinacor.Infra.UI.Controls.SmartClient
{
    public partial class SinacorDataGrid
    {
        #region GetPropertyInfo
        //private List<PropertyValueType> GetPropertyInfo(Object item, List<string> properties)
        //{
        //    List<PropertyValueType> propertiesInfo = new List<PropertyValueType>();

        //    foreach (string property in properties)
        //    {
        //        string[] fieldsName = property.Split('.');

        //        Type propType = item.GetType();
        //        PropertyInfo propInfo = null;
        //        object propValue = item;

        //        foreach (string field in fieldsName)
        //        {
        //            propInfo = propType.GetProperty(field);

        //            if (propInfo != null)
        //            {
        //                propType = propInfo.PropertyType;
        //                propValue = propInfo.GetValue(propValue, null);

        //                if (propType == null || propValue == null)
        //                    break;
        //            }
        //            else
        //            {
        //                propType = null;
        //                propValue = null;
        //                break;
        //            }
        //        }

        //        if (propType != null)
        //        {
        //            PropertyValueType propertyValueType = new PropertyValueType();
        //            propertyValueType.PropertyType = propType;
        //            propertyValueType.PropertyValue = propValue;

        //            propertiesInfo.Add(propertyValueType);
        //        }

        //    }

        //    return propertiesInfo;
        //}
        #endregion
        
        #region FormatProperties
        private object FormatProperties(List<PropertyValueType> propertiesInfo, string stringFormat)
        {
            object propertyValue = null;
            string format = stringFormat;

            if (propertiesInfo.Count > 0)
            {
                if (propertiesInfo.Count > 1)
                {
                    //Não possui StringFormat, monta string default
                    if (String.IsNullOrEmpty(format))
                    {
                        for (int i = 0; i < propertiesInfo.Count; i++)
                        {
                            if (i > 0)
                                format += " ";

                            format += "{" + i + "}";
                        }
                    }

                    object[] propertiesArray = propertiesInfo.ConvertAll<object>(
                                                    delegate(PropertyValueType value)
                                                    {
                                                        return GetPropertyValue(value.PropertyValue);
                                                    }
                                               ).ToArray();
                    propertyValue = string.Format(format, propertiesArray);
                }
                else
                {
                    if (String.IsNullOrEmpty(format))
                    {
                        propertyValue = GetPropertyValue(propertiesInfo[0].PropertyValue);
                    }
                    else
                    {
                        propertyValue = string.Format(format, GetPropertyValue(propertiesInfo[0].PropertyValue));
                    }
                }
            }

            return propertyValue;
        }
        #endregion

        #region FillItemsInfo
        /// <summary>
        /// Alimenta coleção que será disponibilizada nos Eventos ExportingExcel e ExportedExcel
        /// </summary>
        /// <returns></returns>
        private List<ItemInfo> FillItemsInfo(IEnumerable items)
        {
            List<ItemInfo> itemsInfo = new List<ItemInfo>();

            foreach (object item in items)
            {
                // Incluindo na coleção
                itemsInfo.Add(new ItemInfo(this.GetIndex(item), item));
            }

            return itemsInfo;
        }
        #endregion

        #region GetPropertyValue
        private object GetPropertyValue(object value)
        {
            object propertyValue = null;

            //Boleano - Sim ou Não
            if (value is bool ||
                value is bool?)
            {
                if (value != null)
                {
                    if (Convert.ToBoolean(value))
                        propertyValue = (string)ResourceProviderManager.GetResourceProvider().TryGetResource("Infra.Common.Label.Sim");
                    else
                        propertyValue = (string)ResourceProviderManager.GetResourceProvider().TryGetResource("Infra.Common.Label.Nao");
                }
            }
            //EnumWrapper - Retorna Descrição do Enum
            else if (value is IEnumWrapper)
            {
                propertyValue = ((IEnumWrapper)value).Text;
            }
            //Qualquer outro Type - Retorna o mesmo Type
            else
            {
                propertyValue = value;
            }

            return propertyValue;
        }
        #endregion
    }

    //internal class PropertyValueType
    //{
    //    public Object PropertyValue { get; set; }
    //    public Type PropertyType { get; set; }
    //}
}
